"""扫描执行器测试"""
import pytest
from app.scanners.base import ScanExecutor, ScanContext, ScanResult, ScanException
from app.scanners.registry import ScanExecutorRegistry
from app.scanners.harmony_api import HarmonyAPIScanExecutor
from app.scanners.arkts_consistency import ArkTSConsistencyScanExecutor
from app.scanners.cjd_consistency import CJDConsistencyScanExecutor


def test_executor_registry():
    """测试执行器注册"""
    # 检查执行器是否已注册
    assert ScanExecutorRegistry.is_registered("harmony_api_scan")
    assert ScanExecutorRegistry.is_registered("arkts_consistency_scan")
    assert ScanExecutorRegistry.is_registered("cjd_consistency_scan")
    
    # 获取执行器
    executor = ScanExecutorRegistry.create_executor("harmony_api_scan")
    assert executor is not None
    assert isinstance(executor, HarmonyAPIScanExecutor)


def test_harmony_api_executor():
    """测试HarmonyAPI执行器"""
    executor = HarmonyAPIScanExecutor()
    
    assert executor.get_task_type() == "harmony_api_scan"
    assert executor.validate_params({}) is True
    
    # 测试执行扫描
    context = ScanContext(
        scan_record_id=1,
        task_id=1,
        task_type="harmony_api_scan",
        branch="main",
        scan_params={},
    )
    
    result = executor.execute_scan(context)
    assert isinstance(result, ScanResult)
    assert isinstance(result.issues, list)
    assert isinstance(result.summary, dict)
    assert "total_issues" in result.summary
    
    # 检查扩展字段
    supported = executor.get_supported_extensions()
    assert "api_name" in supported
    assert "api_version" in supported


def test_arkts_consistency_executor():
    """测试ArkTS一致性执行器"""
    executor = ArkTSConsistencyScanExecutor()
    
    assert executor.get_task_type() == "arkts_consistency_scan"
    
    context = ScanContext(
        scan_record_id=1,
        task_id=2,
        task_type="arkts_consistency_scan",
        branch="main",
        scan_params={},
    )
    
    result = executor.execute_scan(context)
    assert isinstance(result, ScanResult)
    
    # 检查多代码片段支持
    if result.issues:
        issue = result.issues[0]
        assert "cangjie_code_snippet" in issue or "arkts_code_snippet" in issue
    
    supported = executor.get_supported_extensions()
    assert "cangjie_code_snippet" in supported
    assert "arkts_code_snippet" in supported


def test_cjd_consistency_executor():
    """测试CJD一致性执行器"""
    executor = CJDConsistencyScanExecutor()
    
    assert executor.get_task_type() == "cjd_consistency_scan"
    
    context = ScanContext(
        scan_record_id=1,
        task_id=3,
        task_type="cjd_consistency_scan",
        branch="main",
        scan_params={},
    )
    
    result = executor.execute_scan(context)
    assert isinstance(result, ScanResult)
    
    # 检查多代码片段支持
    if result.issues:
        issue = result.issues[0]
        assert "old_value_code" in issue or "new_value_code" in issue
    
    supported = executor.get_supported_extensions()
    assert "old_value_code" in supported
    assert "new_value_code" in supported

